'use strict';
const webpack = require('webpack');
const path = require('path');
const webpackMerge = require('webpack-merge');
const NyanProgressPlugin = require('nyan-progress-webpack-plugin');
const commonConfig = require('./webpack.common.js');
const pkg = require('../../package.json');
const autoprefixer = require('autoprefixer');
const precss = require('precss');

// 入口文件增加热替换模块
commonConfig.entry.app.unshift(
    'eventsource-polyfill',  // 服务器推送
    'react-hot-loader/patch',
    'webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000',
    'webpack/hot/only-dev-server'
);

// 代码检测器
commonConfig.module.rules.push(      {
    enforce: 'pre',
    test: /\.(js|jsx)$/,
    loader: 'eslint-loader',
    exclude: /node_modules/
});

// 开发环境下直接内嵌 CSS 以支持热替换
commonConfig.module.rules.push({
    test: /\.css$/,
    loader: 'style-loader!css-loader!sass-loader!postcss-loader'
}, {
    test: /\.(scss|sass)$/,
    loader: 'style-loader?sourceMap!postcss-loader!sass-loader'
});

// 混合几个webpack配置
module.exports = webpackMerge(commonConfig, {
    devtool: 'source-map',
    output: {
      publicPath: '/'
    },
    devServer: {
        hot: true,
        inline: true,
        port: pkg.localServer.port,
        host: '0.0.0.0',
        historyApiFallback: true,
        headers:{
            'Access-Control-Allow-Origin': '*'
        },
        watchOptions: {
          aggregateTimeout: 3000,
          poll: 1000
        },
        stats: 'minimal',
        contentBase: path.resolve('src/public')
    },
    plugins: [
        new NyanProgressPlugin(), // 进度条
        new webpack.DefinePlugin({
            'process.env.NODE_ENV': 'development'
        })
    ]
})
